package com.mobi.Survey.map;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

import com.mobi.Survey.lib.activities.AbstractScreen;
import com.mobi.Survey.lib.utils.Helper;

public class MapTabHost extends TabActivity {

	public static final long CLOSE_TIME = 1000;

	private TabHost tabHost;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		final Resources ressources = getResources();
		tabHost = getTabHost();

		Intent intentFavorite = new Intent().setClass(this, FavoritesScreen.class);
		final TabSpec tabSpecFavorites = tabHost.newTabSpec("Favorite")
				.setIndicator("", ressources.getDrawable(R.drawable.favorites_inactive)).setContent(intentFavorite);

		Intent intentBusiness = new Intent().setClass(this, AddBusinessScreen.class);
		final TabSpec tabSpecBusiness = tabHost.newTabSpec("Add Business")
				.setIndicator("", ressources.getDrawable(R.drawable.addbussiness_inactive)).setContent(intentBusiness);

		Intent intentLogin = new Intent().setClass(this, MapLogin.class);
		final TabSpec tabSpecLogin = tabHost.newTabSpec("Search")
				.setIndicator("", ressources.getDrawable(R.drawable.search)).setContent(intentLogin);

		Intent intentSettings = new Intent().setClass(this, AppSettingsScreen.class);
		final TabSpec tabSpecSettings = tabHost.newTabSpec("Settings")
				.setIndicator("", ressources.getDrawable(R.drawable.settings_inactive)).setContent(intentSettings);

		tabHost.addTab(tabSpecFavorites);
		tabHost.addTab(tabSpecBusiness);
		tabHost.addTab(tabSpecLogin);
		tabHost.addTab(tabSpecSettings);

		for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
			tabHost.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg));
		}

		// set Windows tab as default (zero based)

		init();

		int id = AbstractScreen.getMapTabId(this);
		if (id == 0) {

			tabHost.setCurrentTab(id);
			setTabs(0);
			AbstractScreen.setMapTabId(this, -1);
		} else if (id == 1) {
			tabHost.setCurrentTab(id);
			setTabs(1);
			AbstractScreen.setMapTabId(this, -1);
		} else if (id == 2 || id == 22) {
			tabHost.setCurrentTab(2);
			setTabs(2);
			AbstractScreen.setMapTabId(this, -1);
		} else if (id == 3) {
			tabHost.setCurrentTab(3);
			setTabs(3);
			AbstractScreen.setMapTabId(this, -1);
		} else {
			tabHost.setCurrentTab(2);
		}

		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			public void onTabChanged(String arg0) {
				setTabs(tabHost.getCurrentTab());

			}
		});


	}

	private void setTabs(int currentTab) {

		ViewGroup tabInidcator = (ViewGroup) tabHost.getTabWidget().getChildTabViewAt(0);

		ViewGroup tabInidcator2 = (ViewGroup) tabHost.getTabWidget().getChildTabViewAt(1);

		ViewGroup tabInidcator3 = (ViewGroup) tabHost.getTabWidget().getChildTabViewAt(2);

		ViewGroup tabInidcator4 = (ViewGroup) tabHost.getTabWidget().getChildTabViewAt(3);

		switch (currentTab) {
		case 0:
			((ImageView) tabInidcator.getChildAt(0)).setImageDrawable(getResources().getDrawable(R.drawable.favorites));

			((ImageView) tabInidcator2.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.addbussiness_inactive));

			((ImageView) tabInidcator3.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.search_inactive));

			((ImageView) tabInidcator4.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.settings_inactive));

			break;

		case 1:

			((ImageView) tabInidcator.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.favorites_inactive));

			((ImageView) tabInidcator2.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.addbusiness));

			((ImageView) tabInidcator3.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.search_inactive));

			((ImageView) tabInidcator4.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.settings_inactive));

			break;

		case 2:
			((ImageView) tabInidcator.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.favorites_inactive));

			((ImageView) tabInidcator2.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.addbussiness_inactive));

			((ImageView) tabInidcator3.getChildAt(0)).setImageDrawable(getResources().getDrawable(R.drawable.search));

			((ImageView) tabInidcator4.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.settings_inactive));
			break;

		case 3:
			((ImageView) tabInidcator.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.favorites_inactive));

			((ImageView) tabInidcator2.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.addbussiness_inactive));

			((ImageView) tabInidcator3.getChildAt(0)).setImageDrawable(getResources().getDrawable(
					R.drawable.search_inactive));

			((ImageView) tabInidcator4.getChildAt(0)).setImageDrawable(getResources().getDrawable(R.drawable.settings));
			break;

		}

	}

	public final List<Category> readCategories(Context context) {
		List<Category> categories = new ArrayList<Category>();

		InputStream is = null;
		try {
			AssetManager assetManager = context.getAssets();
			is = assetManager.open("BusinessCategories.csv");

			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			// "Active Life","Active Life","1"
			String line = null;
			String previousCategory = "";

			Category category = null;
			while (null != (line = br.readLine())) {

				String split[] = line.split(",");
				if (split.length == 3) {
					String strMainCategory = split[0];
					String strSubCategory = split[1];
					String strId = split[2];

					if (!previousCategory.equalsIgnoreCase(strMainCategory)) {

						if (null != category) {
							Collections.sort(category.getSubCategories());
						}
						category = new Category(Helper.toInt(strId), strMainCategory);
						categories.add(category);

						previousCategory = strMainCategory;
					}
					Category subCategory = new Category(Helper.toInt(strId), strSubCategory);
					if (!categories.contains(subCategory))
						category.getSubCategories().add(subCategory);

				}

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != is) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		Collections.sort(categories);
		return categories;
	}

	private void init() {
		if (MapHelper.categories == null || MapHelper.categories.size() == 0)
			MapHelper.categories = readCategories(this);
	}

	
}